package com.linb.pan.server.modules.share.service.impl;

import com.google.common.collect.Lists;
import com.linb.pan.core.exception.BPanBusinessException;
import com.linb.pan.core.utils.IdUtil;
import com.linb.pan.server.modules.share.context.SaveShareFilesContext;
import com.linb.pan.server.modules.share.entity.BPanShareFile;
import com.linb.pan.server.modules.share.mapper.BPanShareFileMapper;
import com.linb.pan.server.modules.share.service.IShareFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class ShareFileServiceImpl extends ServiceImpl<BPanShareFileMapper, BPanShareFile> implements IShareFileService {

    /**
     * 保存分享的文件的对应关系
     */
    @Override
    public void saveShareFiles(SaveShareFilesContext context) {
        List<BPanShareFile> records = Lists.newArrayList();
        for (Long shareFileId : context.getShareFileIdList()) {
            BPanShareFile record = new BPanShareFile();
            record.setId(IdUtil.get());
            record.setShareId(context.getShareId());
            record.setFileId(shareFileId);
            record.setCreateUser(context.getUserId());
            record.setCreateTime(new Date());
            records.add(record);
        }
        if (!saveBatch(records)) {
            throw new BPanBusinessException("保存文件分享关联关系失败");
        }
    }

}
